/*
 * Copyright (c) 2020, RudyLo <luhuadong@163.com>
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-02-18     luhuadong    the first version
 * 2020-10-05     luhuadong    v0.9.0
 */

#include "dht11service.h"

rt_uint32_t dhtemp = 0;
rt_uint32_t dhhumi = 0;

static void read_humi_entry(void *args)
{
    rt_device_t humi_dev = RT_NULL;
    struct rt_sensor_data sensor_data;

    humi_dev = rt_device_find(args);
    if (!humi_dev)
    {
        rt_kprintf("Can't find humi device.\n");
        return;
    }

    if (rt_device_open(humi_dev, RT_DEVICE_FLAG_RDWR))
    {
        rt_kprintf("Open humi device failed.\n");
        return;
    }

    for (;;)
    {
        if (1 != rt_device_read(humi_dev, 0, &sensor_data, 1))
        {
            //rt_kprintf("Read humi data failed.\n");
        }
        else
        {
            //rt_kprintf("Humi: %d\n",  sensor_data.data.humi);
            dhhumi =  sensor_data.data.humi;
        }

        rt_thread_mdelay(3000);
    }

    rt_device_close(humi_dev);
}

static void read_temp_entry(void *args)
{
    rt_device_t temp_dev = RT_NULL;
    struct rt_sensor_data sensor_data;

    temp_dev = rt_device_find(args);
    if (!temp_dev)
    {
        rt_kprintf("Can't find temp device.\n");
        return;
    }

    if (rt_device_open(temp_dev, RT_DEVICE_FLAG_RDWR))
    {
        rt_kprintf("Open temp device failed.\n");
        return;
    }

    for (;;)
    {
        if (1 != rt_device_read(temp_dev, 0, &sensor_data, 1))
        {
            //rt_kprintf("Read temp data failed.\n");
        }
        else
        {
            //rt_kprintf("Temp: %d\n",sensor_data.data.temp);
            dhtemp = sensor_data.data.temp;
        }

        rt_thread_mdelay(3000);
    }

    rt_device_close(temp_dev);
}

int dht11service_init()
{
    rt_thread_t humi_thread, temp_thread;

    humi_thread = rt_thread_create("humi_th", read_humi_entry,
                                   "humi_dht", 1024,
                                   RT_THREAD_PRIORITY_MAX / 2, 20);

    temp_thread = rt_thread_create("temp_th", read_temp_entry,
                                   "temp_dht", 1024,
                                   RT_THREAD_PRIORITY_MAX / 2, 20);

    if (temp_thread)
    {
        rt_thread_startup(temp_thread);
    }
    else
    {
        rt_kprintf("temp_thread create failed.\n");
        return RT_ERROR;
    }

    if (humi_thread)
    {
        rt_thread_startup(humi_thread);
    }
    else
    {
        rt_kprintf("humi_thread create failed.\n");
        return RT_ERROR;
    }
   
}
